module cl {
	export class TimeEngine {
		public turnForce(time_ref : TimeReference, forceCount : number) : void {
			time_ref.force_of_step = (time_ref.force_of_step + 1) % forceCount;
		}

		public buildNewTime(time_ref : TimeReference) : void {
			let calender = this.buildCalender(time_ref.time, time_ref.time_of_step);

			time_ref.current_step++;
			time_ref.time[2] += time_ref.time_of_step;
			if (time_ref.time[2] > calender[time_ref.time[1]]){
				time_ref.time[2] -= calender[time_ref.time[1]];
				time_ref.time[1]++;
			}
			if (time_ref.time[1] > 12){
				time_ref.time[1] -= 12;
				time_ref.time[0]++;
			}
		}

		private buildCalender(date,ticktime) : number[] {
			let calender = [0,31,28,31,30,31,30,31,31,30,31,30,31];
			if (ticktime < 1){
				if (date[1] != 2 || date[2] < 28){
					return calender;
				}
			}else if (ticktime < 28){
				if (date[1] != 2){
					return calender;
				}
			}
			//maybe cross 2.29
			if (date[0] % 100 == 0){
				if (date[0] % 400 == 0){
					calender[2] = 29;
				}
			}else{
				if (date[0] % 4 == 0){
					calender[2] = 29;
				}
			}
			return calender;
		}
	}
}